“如果說 Live-demo 是跳軌的行為,那麼現學現賣肯定是在臥軌” - 小弱砲
這幾天已經陸續調整了 View 和 ViewModel,使其可讀性上升了一些,然後我將會在原本的程式碼中,引入下方的片段,藉此同時搞定手機端和資料庫端的設定。
FishingSpot
為例do {
let item = FishingSpot(
spotName: "Lorem ipsum dolor sit amet",
imageUrl: "Lorem ipsum dolor sit amet",
recommendationRate: 123.45F,
difficulty: 123.45F,
coordinate: "Lorem ipsum dolor sit amet")
let savedItem = try await Amplify.DataStore.save(item)
print("Saved item: \(savedItem)")
} catch let error as DataStoreError {
print("Error creating item: \(error)")
} catch {
print("Unexpected error: \(error)")
}
Amplify.DataStore.save
。 關於 Async 與 Await 的做法應是為了避免閉塞,之後可以寫一篇來說明。do {
let updatedItem = try await Amplify.DataStore.save(item)
print("Updated item: \(updatedItem)")
} catch let error as DataStoreError {
print("Error updating item: \(error)")
} catch {
print("Unexpected error: \(error)")
}
do {
try await Amplify.DataStore.delete(itemToDelete)
print("Deleted item: \(itemToDelete.identifier)")
} catch let error as DataStoreError {
print("Error deleting item: \(error)")
} catch {
print("Unexpected error: \(error)")
}
do {
let items = try await Amplify.DataStore.query(FishingSpot.self)
for item in items {
print("FishingSpot ID: \(item.id)")
}
} catch let error as DataStoreError {
print("Error querying items: \(error)")
} catch {
print("Unexpected error: \(error)")
}